package struct.primary.linked;

/**
 * @author MAX
 * @version 1.0
 * @description 初级数据结构-链表
 * @date 2022/1/11 16:56
 */
public class LinkedList<E extends Comparable<E>> {

    private Node dummyHead;
    private int size;

    public LinkedList() {
        dummyHead = new Node(null, null);
        size = 0;
    }

    /**
     * 元素个数
     *
     * @return int
     */
    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }
    /**
     * 从头部插入元素
     *
     * @param e 插入元素
     */
    public void addFirst(E e) {
       add(0, e);
    }

    public void addLast(E e) {
        add(size, e);
    }

    /**
     * 指定位置插入元素
     *
     * @param index 位置下标
     * @param e     插入元素
     */
    public void add(int index, E e) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Index-base0 out of boundary");
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);

        size++;
    }

    public E get(int index) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Index-base0 out of boundary");
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        return cur.e;
    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    public boolean contains(E e) {
        if(isEmpty()) throw new RuntimeException("LinkedList Empty");
        Node cur = dummyHead.next;
        while (cur != null) {
            if(e.compareTo(cur.e) != 0) return true;
            cur = cur.next;
        }

        return false;
    }

    public void set(int index, E e) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Index-base0 out of boundary");
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    public E remove(int index) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Index-base0 out of boundary");
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node retNode = prev.next;
        E ret = retNode.e;
        prev.next = retNode.next;
        retNode.next = null;

        return ret;
    }

    public void removeElement(E e) {
        if(isEmpty()) throw new RuntimeException("LinkedList Empty");
        Node prev = dummyHead;
        while(prev.next != null){
            if(prev.next.e.equals(e))
                break;
            prev = prev.next;
        }

        if(prev.next != null){
            Node delNode = prev.next;
            prev.next = delNode.next;
            delNode.next = null;
            size --;
        }
    }

    private class Node {
        public E e;
        public Node next;

        public Node(E e) {
            this(e, null);
        }

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null) {
            res.append(cur.e).append("->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }
}
